
const Mock = require('mockjs')
const user = Mock.mock({
  id: '@id',
  username: '@FIRST',
  timestamp: '@datatime',
  email: '@email',
  display_time: '@datetime',
  pageviews: '@integer(300, 5000)'
})

const data = Mock.mock({
  'items|10': [{
    id: '@id',
    username: '@FIRST',
    timestamp: '@datetime',
    email: '@email',
    phone: '@phone',
    display_time: '@datetime',
    pageviews: '@integer(300, 5000)'
  }]
})

const tokens = {
  admin: 'admin-token'

}

const users = {
  'admin-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Super Admin'
  },
  'editor-token': {
    roles: ['editor'],
    introduction: 'I am an editor',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Normal Editor'
  }
}

module.exports = [
  // user login
  {
    url: '/login',
    type: 'post',
    response: config => {
      const { username } = config.body
      const token = tokens[username]

      // mock error
      if (!token) {
        return {
          code: 401,
          msg: 'Account and password are incorrect.'
        }
      }

      return {
        code: 200,
        data: {

        },
        token,
        msg: '登录成功'
      }
    }
  },

  // get user info
  {
    url: '/getInfo\.*',
    type: 'get',
    response: config => {
      const token = config[Reflect.ownKeys(config).find(k => k.toString() === 'Symbol(kHeaders)')].authorization.slice(7)
      const info = users[token]
      // mock error
      if (!info) {
        return {
          code: 500,
          msg: 'Login failed, unable to get user details.'
        }
      }

      return {
        code: 200,
        user: {
          nickName: 'abner',
          avatar: ''
        },
        msg: '成功'
      }
    }
  },

  // user logout
  {
    url: '/vue-admin-template/user/logout',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  },
  // user table
  {
    url: '/vue-admin-template/users',
    type: 'get',
    response: config => {
      const items = data.items
      return {
        code: 20000,
        data: {
          total: items.length,
          items: items
        }
      }
    }
  },
  // user info
  {
    url: '/vue-admin-template/user\.*',
    type: 'get',
    response: config => {
      const item = user
      return {
        code: 20000,
        data: item
      }
    }
  },
  // save user
  {
    url: '/vue-admin-template/user',
    type: 'put',
    response: config => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  },
  {
    url: '/captchaImage',
    type: 'get',
    response: config => {
      return {
        code: 200,
        data: {}
      }
    }
  }
]

